package org.dromara.web.plugins.provider.imageImpl;


import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisOutput;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.mongo.ChatMessageMongoDao;
import org.dromara.common.mongo.SessionMongoDao;
import org.dromara.dao.business.domain.Model;
import org.dromara.dao.business.domain.ModelParam;
import org.dromara.web.plugins.provider.TextToImageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class AliyunDashScopeProvider implements TextToImageProvider {


    public static final String MODEL_TYPE = "aliyundashscope";

    private static final String URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis";
    private static final String API_KEY = "sk-6c0f22ba9ecd4ccd839a4579716cd252";

    @Autowired
    OkHttpClient okHttpClient;
    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    SessionMongoDao sessionMongoDao;
    @Autowired
    ChatMessageMongoDao chatMessageMongoDao;

    public static final Map<String, String> sizeMap = new HashMap<>();
    static {
        sizeMap.put("9:21", "576*1344");
        sizeMap.put("9:16", "576*1024");
        sizeMap.put("2:3", "1024*1536");
        sizeMap.put("4:5", "1024*1280");
        sizeMap.put("1:1", "1024*1024");
        sizeMap.put("5:4", "1280*1024");
        sizeMap.put("3:2", "1536*1024");
        sizeMap.put("16:9", "1024*576");
        sizeMap.put("21:9", "1344*576");
    }


    @Override
    public String generateImage(Model model, String prompt, ModelParam modelParam, String scale) {
//
//        FormBody.Builder builder = new FormBody.Builder();
//        builder.add("model", "wanx-v1");
//        builder.add("prompt", prompt);
//        builder.add("size", sizeMap.get(scale));
//        builder.add("response_format", "b64_json");
//
//
//        RequestBody requestBody = builder.build();
//
//        Request request = new Request.Builder()
//            .url(URL)
//            .header("Authorization", "Bearer " + API_KEY)
//            .post(requestBody)
//            .build();
//
//
//        try {
//            Response response = okHttpClient.newCall(request).execute();
//            if (response.isSuccessful()) {
//
//                JSONObject jsonObject = JSONObject.parseObject(response.body().string());
//
//
//            }
//            else {
//                throw new ServiceException("服务异常");
//            }
//        } catch (Exception e) {
//            throw new ServiceException("服务异常");
//        }
//        return null;


        ImageSynthesisParam param = ImageSynthesisParam
                .builder()
                .apiKey(API_KEY)
                .model(model.getValue())
                .prompt(prompt)
                .n(1)
                .size(sizeMap.get(scale))
                .build();

        ImageSynthesis imageSynthesis = new ImageSynthesis();
        ImageSynthesisResult result = null;
        try {
            result = imageSynthesis.call(param);
            ImageSynthesisOutput output = result.getOutput();
            if (output != null) {
                List<Map<String, String>> results = output.getResults();
                if (CollectionUtils.isNotEmpty(results)) {
                    Map<String, String> resultMap = results.get(0);
                    return resultMap.get("url");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
